package com.dreamer.repository.mobile;

import com.dreamer.domain.mall.transfer.Transfer;
import com.dreamer.domain.user.User;
import com.wxjssdk.util.DateUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import ps.mx.otter.utils.SearchParameter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by huangfei on 01/07/2017.
 */
@Repository
public class TransferDao extends BaseDaoImpl<Transfer> {

    private Session getCurrentSession() {
        return getSessionFactory().getCurrentSession();
    }

    public List<Transfer> findRecords(SearchParameter<Transfer> p, User currentUser) {
        Example example = Example.create(p.getEntity());
//        p.getEntity().setNotShow(false);//可以展示
        DetachedCriteria dc = DetachedCriteria.forClass(Transfer.class);
        dc.add(example);
        if (p.getEntity().getToAgent() != null&&!p.getEntity().getToAgent().getRealName().equals("")) {
            DetachedCriteria toAgent = dc.createCriteria("toAgent");
            String code= p.getEntity().getToAgent().getRealName();
            toAgent.add(Restrictions.or(Restrictions.eq("realName", p.getEntity().getToAgent().getRealName()), Restrictions.eq("agentCode", code)));
        }
        if (currentUser.isAdmin()) {//查询所有
            if (p.getEntity().getFromAgent() != null&&!p.getEntity().getFromAgent().getRealName().equals("")) {
                String code1= p.getEntity().getToAgent().getRealName();
                DetachedCriteria fromAgent = dc.createCriteria("fromAgent");
                fromAgent.add(Restrictions.or(Restrictions.eq("realName", p.getEntity().getFromAgent().getRealName()), Restrictions.eq("agentCode", code1)));
            }
        } else {
            dc.add(Restrictions.or(Restrictions.eq("fromAgent.id", currentUser.getId()), Restrictions.eq("toAgent.id", currentUser.getId())));
        }
        if (p.getStartTime() != null && !p.getStartTime().equals("")) {
            dc.add(Restrictions.between("applyTime", p.getStartTimeByDate(), p.getEndTimeByDate()));
        }
        dc.addOrder(Order.desc("id"));//id排序
        return searchByPage(p, dc);
    }

    public List<Transfer> findChildrenTransferByPid(Integer pid){
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-06-01");
        }catch (Exception e){
            e.printStackTrace();
        }
        String hql = "from Transfer as t where (t.fromAgent.parent.id = :pid or t.toAgent.parent.id = :pid) and t.updateTime >= :date ";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("pid",pid);
        query.setParameter("date",date);
        return query.list();
    }

    public List<Transfer> findTransfer(Integer uid){
        Date date = new Date();
        Date endDate = new Date();
        try {
            date = DateUtil.formatStartTime("2018-06-29");
            endDate = DateUtil.formatEndTime("2018-07-28");
        }catch (Exception e){
            e.printStackTrace();
        }
        String hql = "from Transfer as t where (t.toAgent.id = :uid or t.fromAgent.id = :uid) and t.updateTime >= :date and t.updateTime <=:endDate";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("uid",uid);
        query.setParameter("date",date);
        query.setParameter("endDate",endDate);
        return query.list();
    }

    public List<Transfer> findTransfer(Integer pid,List<Integer> cids){
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-05-15");
        }catch (Exception e){
            e.printStackTrace();
        }
        String hql = "from Transfer as t where ((t.toAgent.id = :pid and t.fromAgent.id in :cids) or ((t.fromAgent.id = :pid and t.toAgent.id in :cids)))  and t.updateTime >= :date ";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("pid",pid);
        query.setParameter("cids",cids);
        query.setParameter("date",date);
        return query.list();
    }

    public List<Transfer> findTransfer(List<Integer> cids){
        Date date = new Date();
        Date endDate = new Date();
        try {
            date = DateUtil.formatStartTime("2018-06-29");
            endDate = DateUtil.formatEndTime("2018-07-28");
        }catch (Exception e){
            e.printStackTrace();
        }
        String hql = "from Transfer as t where (t.fromAgent.id in :cids or  t.toAgent.id in :cids)  and t.updateTime >= :date and t.updateTime <= :endDate ";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("cids",cids);
        query.setParameter("date",date);
        query.setParameter("endDate",endDate);
        return query.list();
    }


    public List<Transfer> findByToAgent(Integer toId){
        String hql = "from Transfer as t where t.toAgent.id = :toId GROUP BY t.fromAgent.id ORDER BY t.updateTime DESC";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("toId",toId);
        return query.list();
    }
    public List<Transfer> findByFromAgent(Integer fromId){
        String hql = "from Transfer as t where t.fromAgent.id = :fromId GROUP BY t.toAgent.id ORDER BY t.updateTime DESC";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("fromId",fromId);
        return query.list();
    }


}
